package com.duolaibei.pay.base;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.pay.R;
import com.duolaibei.pay.activity.HomepageActivity;
import com.duolaibei.pay.utils.NetUtil;
import com.duolaibei.pay.utils.ToastUtils;

import butterknife.ButterKnife;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by wangshuo on 2017/5/13.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    RelativeLayout back; //返回
    TextView text;//文本
    protected TextView serach;//右边文本
    String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewResId());
        ButterKnife.bind(this);
        initToolbar();
        if (EasyPermissions.hasPermissions(this, perms)) {
            intiViews();
        } else {
            EasyPermissions.requestPermissions(this, getString(R.string.sdcard_permissions_tips), 1, perms);
        }
        onNetChange();
    }

    /**
     * 继承类实现，返回布局文件 布局文件必须包含公共的titlebar
     *
     * @return
     */
    public abstract int getContentViewResId();

    public abstract void intiViews();

    /**
     * 设置text
     *
     * @return
     */
    public abstract String setTitle();


    protected void initToolbar() {
        back = (RelativeLayout) findViewById(R.id.back);
        text = (TextView) findViewById(R.id.text);
        serach = (TextView) findViewById(R.id.serach);
        if (null == back || null == text) {
            return;
        }
        if (null != setTitle()) {
            text.setText(setTitle());
        }
        back.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.back)
            this.finish();
    }

    public boolean onNetChange() {
        if (NetUtil.getNetworkState(this) == NetUtil.NETWORN_NONE) {
            ToastUtils.showToast(this, "手机没有网络连接", 3000);
            return false;
        }
        return true;
    }

}
